<?php
namespace app\admin\controller\system\adminuser;

use app\common\model\AdminUser as Model;
use app\common\model\AdminRole as Model_Role;

use app\admin\classes\BasicManager;
use core\Api;
use core\V;
use core\Res;
use core\Func;

/**
 * 后台用户管理
 */


 class AdminUserManager extends BasicManager {

   static $model=Model::class;
   static $model_role=Model_Role::class;


   static $var="id,username,password,nickname,email,role,mobile,avatarUrl,status,remark";

   static $title=[
     "index"=>[
       "title"=>"后台用户管理",
       "breadcrumb"=>["系统设置","后台用户","列表"],
       "subtitle"=>"管理后台用户",
     ],
     "add"=>[
       "title"=>"添加后台用户",
       "breadcrumb"=>["系统设置","后台用户","添加"],
       "subtitle"=>"添加新的后台用户",
     ],
     "edit"=>[
       "title"=>"编辑后台用户",
       "breadcrumb"=>["系统设置","后台用户","编辑"],
       "subtitle"=>"编辑后台用户信息",
     ],

   ];

   protected function getRoleName($id){
     static $_store;
     if (empty($_store)){
       $_store=static::$model_role::column('name','id');
     }


     $ids=Func::json2arr($id);
     $result=[];
     foreach ($ids as $v) {
       if (isset($_store[$v])){
         $result[]=$_store[$v];
       }
     }

     return Func::arr2str($result," , ");
   }
   protected function getRoleList(){
     $roles=static::$model_role::where("status",1)
                  ->column('name','id');
     $result=[];
     foreach ($roles as $k=>$v) {
       $result[]=[
         "key"=>(string)$k,
         "label"=>(string)$v,
       ];
     }

     return Func::arr2json($result);
   }

   public function getlist_search(&$query){
     $query->removeOption('where');
     return parent::getlist_search($query);
   }


   public function getlist_filter(&$item){

     return [
       "password"=>"",
       "role"=>$this->getRoleName($item->role),
     ];
   }
   public function add_filter(){
     return [
       "act"=>"add",
       "status"=>1,
       "role"=>[],
     ];
   }
   public function edit_filter(&$item){
     return [
       "role"=>Func::json2arr($item["role"]),
     ];
   }
   public function save_filter(&$data){
     empty($data["username"]) and Api::error('请输入用户名');
     empty($data["nickname"]) and Api::error('请输入昵称');

     $act=input("act");
     if ($act=="add"){
       if (Model::where("username",$data["username"])->count()){
         Api::error('用户名已存在');
       }
       empty($data["password"]) and Api::error('请输入密码');

       $data["password"]=md5($data["password"]);
     }else{
       unset($data["username"]);
       unset($data["password"]);
     }

     $data["role"]=Func::arr2json($data["role"]);

     return $data;
   }

   public function changepassword(){
     $id=input("id");
     $password=input("password");
     empty($id) and Api::error('id错误');
     empty($password) and Api::error('请输入新密码');

     $item=Model::where("id",$id)->find();
     if ($item){
       $item->password=md5($password);
       if ($item->save()){
         Api::ok('密码重置成功');
       }else{
         Api::error('遇到错误');
       }
     }else{
       Api::error('没有找到记录');
     }
   }

   public function add_body(){
     $this->VD([
       "___vue___"=>'
       data() {
         return {
           roleList:'.$this->getRoleList().'
         }
       }
       '
     ]);
     return <<<'CODE'
     <el-form-item label="用户名">
       <el-input v-model="form.username"></el-input>
     </el-form-item>

     <el-form-item label="密码">
       <el-input type="password" v-model="form.password"></el-input>
     </el-form-item>

     <el-form-item label="昵称">
       <el-input v-model="form.nickname"></el-input>
     </el-form-item>

     <el-form-item label="Email">
       <el-input v-model="form.email"></el-input>
     </el-form-item>

     <el-form-item label="手机">
       <el-input v-model="form.mobile"></el-input>
     </el-form-item>

     <el-form-item label="角色">
        <el-transfer
          :titles="['所有角色','我的角色']"
          v-model="form.role"
          :data="roleList">
        </el-transfer>
     </el-form-item>

     <el-form-item label="头像">
       <c-upload-image
         v-model="form.avatarUrl"
         action="/admin/upload/image"
         limit="1"
       >
       </c-upload-image>
     </el-form-item>

     <el-form-item label="是否启用">
       <c-status v-model="form.status"></c-status>
     </el-form-item>

     <el-form-item label="备注">
       <el-input type="textarea" v-model="form.remark"></el-input>
     </el-form-item>
CODE;
   }
   public function edit_body(){
     $this->VD([
       "___vue___"=>'
       data() {
         return {
           roleList:'.$this->getRoleList().'
         }
       },
       methods: {
         doChangePWD:function(id){
           var that=this;
           this.$prompt("请输入新密码", "重置密码", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              inputPlaceholder:"输入新密码",
              inputType:"password",
              callback:function(action,instance){
                if (action=="confirm"){
                  var passwd=instance.inputValue;
                  if (passwd){
                    APP.ajax(that.apiBase+"/changepassword",{id:id,password:passwd},function(msg){
                      if (msg.result){
                        APP.alert(msg.data,true);
                      }else{
                        APP.alert(msg.data);
                      }
                    });
                  }
                }
              }
            });
         }

       }
       '
     ]);

     return <<<'CODE'
     <el-form-item label="用户名">
       <el-input
        v-model="form.username"
        :disabled="true">
      </el-input>
     </el-form-item>

     <el-form-item label="密码">
       <el-button
        @click="doChangePWD(form.id)"
        >重置密码</el-button>
     </el-form-item>

     <el-form-item label="昵称">
       <el-input v-model="form.nickname"></el-input>
     </el-form-item>

     <el-form-item label="Email">
       <el-input v-model="form.email"></el-input>
     </el-form-item>

     <el-form-item label="手机">
       <el-input v-model="form.mobile"></el-input>
     </el-form-item>

     <el-form-item label="角色">
        <el-transfer
          :titles="['所有角色','我的角色']"
          v-model="form.role"
          :data="roleList">
        </el-transfer>
     </el-form-item>

     <el-form-item label="头像">
       <c-upload-image
         v-model="form.avatarUrl"
         action="/admin/upload/image"
         limit="1"
       >
       </c-upload-image>
     </el-form-item>

     <el-form-item label="是否启用">
       <c-status v-model="form.status"></c-status>
     </el-form-item>

     <el-form-item label="备注">
       <el-input type="textarea" v-model="form.remark"></el-input>
     </el-form-item>
CODE;
   }
   public function index_body(){
     static::$actArea_width="120px";
     $this->VD([
       "___css___"=>'
        .tablebtn-del{
          display:none;
        }
       ',
       "___vue___"=>'
       data() {
         return {
           roleList:[]
         }
       },
       methods: {
         doChangePWD:function(id){
           var that=this;
           this.$prompt("请输入新密码", "重置密码", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              inputPlaceholder:"输入新密码",
              inputType:"password",
              callback:function(action,instance){
                if (action=="confirm"){
                  var passwd=instance.inputValue;
                  if (passwd){
                    APP.ajax(that.apiBase+"/changepassword",{id:id,password:passwd},function(msg){
                      if (msg.result){
                        APP.alert(msg.data,true);
                      }else{
                        APP.alert(msg.data);
                      }
                    });
                  }
                }
              }
            });
         }

       }
       ',
       "___btn___"=>'

         <el-button
          @click="doChangePWD(scope.row.id)"
          type="text" size="small">重置密码</el-button>


       ',
     ]);
     return <<<'CODE'
     <el-table-column
       fixed="left"
       prop="id"
       label="ID"
       width="60">
     </el-table-column>

     <el-table-column
       fixed="left"
       prop="username"
       label="用户名"
       width="150">
     </el-table-column>

     <el-table-column
       prop="nickname"
       label="昵称"
       width="150">
     </el-table-column>

     <el-table-column
       prop="email"
       label="Email"
       width="180">
     </el-table-column>

     <el-table-column
       prop="mobile"
       label="手机"
       width="120">
     </el-table-column>

     <el-table-column
       prop="role"
       label="角色"
       width="180">
     </el-table-column>

     <el-table-column
       prop="remark"
       label="备注"
       min-width="120"
       >
     </el-table-column>

     <el-table-column
       prop="status"
       label="状态"
       width="60">
       <template slot-scope="scope">
        <el-tag
          v-if="scope.row.status==1"
          size="mini"
          type="success">启用</el-tag>
        <el-tag
          v-if="scope.row.status==2"
          size="mini"
          type="danger">禁用</el-tag>
       </template>
     </el-table-column>
CODE;
   }
   public function search_body(){
     return '
     <el-form-item label="">
       <el-input
         v-model="form.name"
         @keyup.enter.native="Search"
         placeholder="名称">
       </el-input>
     </el-form-item>
     <el-form-item label="">
       <el-input
         v-model="form.remark"
         @keyup.enter.native="Search"
         placeholder="备注">
       </el-input>
     </el-form-item>
     <el-form-item label="">
       <el-select @change="Search" v-model="form.status" placeholder="状态" style="width:80px;">
         <el-option label="全部" value=""></el-option>
         <el-option label="启用" value="1"></el-option>
         <el-option label="禁用" value="2"></el-option>
       </el-select>
     </el-form-item>
     ';

   }



 }
